﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DDNS.Infrastructure
{
    /// <summary>
    /// 全局应用配置
    /// </summary>
    public static class PassportConfig
    {
        /// <summary>
        /// 
        /// </summary>
        public static IConfiguration Configuration;

        /// <summary>
        /// 
        /// </summary>
        public static IHostEnvironment Env;

        public static void InitPassportConfig(this IConfiguration config, IHostEnvironment environment)
        {
            if (Configuration == null)
            {
                Configuration = config;
                Env = environment;
            }
        }
        public static bool IsDevelopment() => Env.IsDevelopment();
        public static bool IsStaging() => Env.IsStaging();
        public static bool IsProduction() => Env.IsProduction();

        /// <summary>
        /// 获取单个简单配置
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Get(string key)
        {
            return Configuration[key];
        }

        /// <summary>
        /// 获取单个实体配置
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T Get<T>(string key) where T : class
        {
            var section = GetSection(key);
            return section != null ? section.Get<T>() : default(T);
        }

        public static bool GetBool(string key)
        {
            bool isTrue = false;
            if (!string.IsNullOrEmpty(key) && Configuration != null)
            {
                bool.TryParse(Get(key), out isTrue);
            }
            return isTrue;
        }

        public static int GetInt(string key)
        {
            int intValue = 0;
            if (!string.IsNullOrEmpty(key) && Configuration != null)
            {
                int.TryParse(Get(key), out intValue);
            }
            return intValue;
        }

        public static int GetInt(string key, int defaultValue = int.MinValue)
        {
            int intValue = 0;
            if (!string.IsNullOrEmpty(key) && Configuration != null)
            {
                var parseResult = int.TryParse(Get(key), out intValue);
                if (parseResult == false && defaultValue != int.MinValue)
                {
                    return defaultValue;
                }
            }
            return intValue;
        }

        /// <summary>
        /// 获取某个section中指定key的配置
        /// </summary>
        /// <param name="key"></param>
        /// <param name="valIndex"></param>
        /// <returns></returns>
        public static string GetSection(string key, string valIndex)
        {
            return Configuration.GetSection(key)[valIndex];
        }

        /// <summary>
        ///  获取指定section
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IConfigurationSection GetSection(string key)
        {
            return Configuration.GetSection(key);
        }

        /// <summary>
        ///  获取指定section
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetSection<T>(string key)
        {
            return Configuration.GetSection(key).Get<T>();
        }

        /// <summary>
        /// 获取数据库连接字符串
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetConnectionString(string name)
        {
            return Configuration.GetConnectionString(name);
        }

        /// <summary>
        /// 获取命令行 --healthHost 参数
        /// </summary>
        /// <returns></returns>
        public static string GetHealthHost()
        {
            return Get("healthHost") ?? throw new ArgumentNullException("healthHost cannot be null or empty!"); ;
        }

        /// <summary>
        /// 获取当前运行程序的端口
        /// </summary>
        /// <returns></returns>
        public static int GetCurrentPort()
        {
            var launchUrl = (PassportConfig.Get("urls") ?? "http://[::]:5000").Replace("*", "[::]").Replace("+", "[::]");
            return new Uri(launchUrl).Port;
        }

    }
}
